import type { Recordable } from '@vben/types';
import type { PagedList, PagedRequest } from '../types';

import { requestClient } from '#/api/request';

export namespace RoleApi {
  /**
   * 角色列表项
   */
  export interface RoleItem {
    id: string;
    name: string;
    description?: string;
    isSystem: boolean;
    createdTime: string;
    status: number;
  }

  /**
   * 角色详情
   */
  export interface RoleDetail {
    id: string;
    name: string;
    description?: string;
    isSystem: boolean;
    concurrencyStamp: string;
    permissionIds: string[];
    createdTime: string;
    lastModifiedTime?: string;
  }

  /**
   * 角色创建参数
   */
  export interface RoleCreate {
    name: string;
    description?: string;
    isSystem?: boolean;
    permissionIds?: string[];
  }

  /**
   * 角色更新参数
   */
  export interface RoleUpdate {
    id: string;
    name: string;
    description?: string;
    concurrencyStamp: string;
    permissionIds?: string[];
  }

  /**
   * 角色选择项
   */
  export interface RoleSelect {
    id: string;
    name: string;
  }

  /**
   * 查询参数
   */
  export interface RoleQuery extends PagedRequest {
    name?: string;
    isSystem?: boolean;
  }

  /**
   * 角色用户分配参数
   */
  export interface RoleAssignUsers {
    roleId: string;
    userIds: string[];
  }

  /**
   * 角色批量删除参数
   */
  export interface RoleBatchDelete {
    roleIds: string[];
  }
}

/**
 * 获取角色分页列表
 * @param params 查询参数
 */
export async function getRolePagedList(params: RoleApi.RoleQuery) {
  return requestClient.get<PagedList<RoleApi.RoleItem>>(
    '/roles',
    { params },
  );
}

/**
 * 获取角色详情
 * @param id 角色ID
 */
export async function getRoleById(id: string) {
  return requestClient.get<RoleApi.RoleDetail>(
    `/roles/${id}`,
  );
}

/**
 * 获取角色选择列表
 */
export async function getRoleSelectList() {
  return requestClient.get<RoleApi.RoleSelect[]>(
    '/roles/select',
  );
}

/**
 * 创建角色
 * @param data 角色数据
 */
export async function createRole(data: RoleApi.RoleCreate) {
  return requestClient.post<string>(
    '/roles',
    data,
  );
}

/**
 * 更新角色
 * @param id 角色ID
 * @param data 角色数据
 */
export async function updateRole(id: string, data: RoleApi.RoleUpdate) {
  return requestClient.put<void>(
    `/roles/${id}`,
    data,
  );
}

/**
 * 删除角色
 * @param id 角色ID
 */
export async function deleteRole(id: string) {
  return requestClient.delete<void>(
    `/roles/${id}`,
  );
}

/**
 * 批量删除角色
 * @param roleIds 角色ID列表
 */
export async function batchDeleteRoles(roleIds: string[]) {
  return requestClient.post<string[]>(
    '/roles/batch-delete',
    { roleIds },
  );
}

/**
 * 检查角色名是否存在
 * @param roleName 角色名
 * @param excludeRoleId 排除的角色ID
 */
export async function checkRoleName(roleName: string, excludeRoleId?: string) {
  return requestClient.get<boolean>(
    '/roles/check-name',
    { params: { roleName, excludeRoleId } },
  );
}

/**
 * 获取角色用户列表
 * @param roleId 角色ID
 */
export async function getRoleUsers(roleId: string) {
  return requestClient.get<any[]>(
    `/roles/${roleId}/users`,
  );
}

/**
 * 为角色分配用户
 * @param data 分配参数
 */
export async function assignUsersToRole(data: RoleApi.RoleAssignUsers) {
  return requestClient.post<void>(
    '/roles/assign-users',
    data,
  );
}

/**
 * 从角色中移除用户
 * @param data 移除参数
 */
export async function removeUsersFromRole(data: RoleApi.RoleAssignUsers) {
  return requestClient.post<void>(
    '/roles/remove-users',
    data,
  );
}


